package edu.whu.demo.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import edu.whu.demo.domain.Schedule;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 
 * @since 2022-11-24
 */
@Mapper
public interface ScheduleDao extends BaseMapper<Schedule> {

    @Select("select * from schedule where id=#{id}")
    Schedule findScheduleById(long id);

    @Select("select DISTINCT label FROM schedule")
    List<Schedule> findScheduleLabel();

    @Select("select * from schedule where activity_id=#{activiyId}")
    List<Schedule> findScheduleByActivityId(Long activiyId);

    /*全文搜索索引，涉及column：`title`,`begin_time`,`end_time`,`location`,`memo`*/
    @Select("select distinct s.* from schedule s"+
            "where match(`title`,`begin_time`,`end_time`,`location`,`memo`) AGAINST (#{keyword}) and uid={#uid};"
    )
    public IPage<Schedule> findScheduleByInfo(IPage<Schedule> page, String keyword,Long uid);

   /* *//*多表查询，和活动表*//*
    @Select("select distinct s.* from schedule s "+
            "left join activity a on s.activity_id = a.id "+
            "${ew.customSqlSegment}"
    )
    @Results({@Result(id = true,property = "id",column = "id"),
            @Result(property = "activityId",column = "id",
                    one = @One(select = "edu.whu.demo.dao.ActivityDao.selectById"))})
    public IPage<Schedule> findScheduleByActivity(IPage<Schedule> page, @Param(Constants.WRAPPER) QueryWrapper<Schedule> wrapper);
*/
    /*多表查询，和用户表*/
    @Select("select distinct s.* from schedule s"+
            "left join user u on s.uid = u.uid"+
            "${ew.customSqlSegment}"
    )
    @Results({@Result(id = true,property = "id",column = "id"),
            @Result(property = "uid",column = "uid",
                    one = @One(select = "edu.whu.demo.dao.UserDao.findUserByUid"))})
    public IPage<Schedule> findScheduleByUser(IPage<Schedule> page, @Param(Constants.WRAPPER) QueryWrapper<Schedule> wrapper);

    @Select("select distinct s.* from schedule s "+
            "${ew.customSqlSegment}"
    )
    public IPage<Schedule> findSchedule(IPage<Schedule> page, @Param(Constants.WRAPPER) QueryWrapper<Schedule> wrapper);


}
